﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using Mimoza.Common;


namespace Mimoza.Plugins.DisplaySettings
{
    public enum ColorDepth
    {
        Color8Bit = 0,
        Color16Bit,
        Color24Bit,
        Color32Bit
    };

    //public enum Model
    //{
    //    Undefined = (-1),
    //    L66 = 0,
    //    C36SX,
    //    LX,
    //    VirtualPC
    //};
   
    public class DisplayResolution
    {
        private int m_width;
        private int m_height;
        private int m_freq;
        private ColorDepth m_colorDepth;
        private ModelEnum m_model = ModelEnum.Undefined;

        public DisplayResolution()
        {
            m_width = 800;
            m_height = 600;
            m_freq = 60;
            m_colorDepth = ColorDepth.Color16Bit;
        }
        public DisplayResolution(int _width, int _height, int _freq, ColorDepth _colorDepth)
        {
            m_width = _width;
            m_height = _height;
            m_freq = _freq;
            m_colorDepth = _colorDepth;
        }

        public DisplayResolution(string settings)
        {
            string[] str = settings.Split(new char[]{'X','x','-','H','z'});

            m_width = Int32.Parse(str[0]);
            m_height = Int32.Parse(str[1]);
            m_freq = Int32.Parse(str[3]);

            int color = Int32.Parse(str[2]);
            switch (color)
            { 
                case 8:
                    m_colorDepth = ColorDepth.Color8Bit;
                    break;
                case 16:
                    m_colorDepth = ColorDepth.Color16Bit;
                    break;
                default:
                    m_colorDepth = ColorDepth.Color32Bit;
                    break;
            }
        }

        public ModelEnum Model
        {
            get { return m_model; }
            set { m_model = value; }
        }

        public int Width
        {
            get
            {
                return m_width;
            }
            set
            {
                m_width = value;
            }
        }
        public int Height
        { 
            get
            {
                return m_height;
            }
            set
            {
                m_height = value;
            }
        }
        public int Freq
        {
            get
            {
                return m_freq;
            }
            set
            {
                m_freq = value;
            }
        }
        public ColorDepth ColorDepth
        {
            get
            {
                return m_colorDepth;
            }
            set
            {
                m_colorDepth = value;
            }
        }
        public int ColorDepthAsInt
        {
            get
            {
                int depth = 8 * ((int)m_colorDepth + 1);
                return depth;
            }
        }
        public double Ratio
        {
            get 
            {
                return (double)m_width / (double)m_height;
            }
        }
        public string DisplayText
        {
            get
            {
                return m_width.ToString() + " x " + m_height.ToString();
            }
        }
        public string DisplayResolutionText
        {
            get
            {
                //640x480x16-60Hz
                int colorDepth;
                switch (m_colorDepth)
                { 
                    case ColorDepth.Color8Bit:
                        colorDepth = 8;
                        break;
                    case ColorDepth.Color16Bit:
                        colorDepth = 16;
                        break;
                    default:
                        colorDepth = 32;
                        break;
                }
                return m_width.ToString() + "x" + m_height.ToString() + "x" + colorDepth.ToString() + "-" + m_freq.ToString() + "Hz";
            }
        }
 
        public string Serialize()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(DisplayResolution));
            TextWriter text = new StringWriter();
            serializer.Serialize(text, this);
            return text.ToString();
        }
        public static DisplayResolution Deserialize(string data)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(DisplayResolution));
            using (TextReader text = new StringReader(data))
            {
                return deserializer.Deserialize(text) as DisplayResolution;
            }
        }
    }

    public class DisplayBackground
    {
        private Image m_background;
        public Image Background
        {
            get
            {
                return m_background;
            }
            set
            {
                m_background = value;
            }
        }

        public string Serialize()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(DisplayBackground));
            TextWriter text = new StringWriter();
            serializer.Serialize(text, this);
            return text.ToString();
        }
        public static DisplayBackground Deserialize(string data)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(DisplayBackground));
            using (TextReader text = new StringReader(data))
            {
                return deserializer.Deserialize(text) as DisplayBackground;
            }
        }
    }
}
